//
//  Status.swift
//  weibo
//
//  Created by wang_h on 2017/6/1.
//  Copyright © 2017年 wang_h. All rights reserved.
//

import UIKit

class Status: NSObject {
    
    var created_at : String? {       //微博创建时间
        didSet{
            guard let create_at = created_at else {
                return
            }
            createAtText = NSDate.createTimeAt(createAtStr: create_at)
        }
    }
    var mid : Int = 0               //微博来源
    var text : String?              //微博的正文
    var source : String? {          //微博的ID
        //1.nil校验
        didSet{
            guard let source = source, source != "" else { return }
            //2.对来源的字符串进行处理
            //2.1.获取起始位置和截取长度
            //<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">微博 weibo.com</a>
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            //2.2截取字符串
            sourceText = (source as NSString).substring(with: NSRange(location: startIndex, length: length))
        }
    }
    //MARK:- 对数据处理的属性
    var sourceText : String?
    var createAtText : String?
    
    var user : User?
    
    //MARK:- 自定义构造函数
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeys(dict)
        
        //将用户字典转成用户模型对象
        if let userDict = dict["user"] as? [String : AnyObject]{
            user = User(dict: userDict)
        }
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
}
